﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;

namespace TdyxAutoBot
{
    /// <summary>
    /// 窗体单击确定按钮代理
    /// </summary>
    /// <param name="account"></param>
    public delegate void AddAcountFormOK(Account account);

    public partial class AddAcountForm : Form
    {
        private List<Account> accountList = null;
        private Account account = null;

        public AddAcountForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 显示增加窗口
        /// </summary>
        /// <param name="accountList"></param>
        public void ShowAdd(List<Account> accountList)
        {
            this.accountList = accountList;
            this.Text = "添加账户";
            this.ShowDialog();
        }

        /// <summary>
        /// 显示修改窗口
        /// </summary>
        /// <param name="account"></param>
        public void ShowEdit(Account account)
        {
            this.account = account;
            this.textBoxChanel.Text = account.Chanel.ToString();
            this.textBoxCharacterName.Text = account.Character.Name;
            this.textBoxPassword.Text = account.Password;
            this.textBoxSecureCode.Text = account.SecureCode;
            this.textBoxServerID.Text = account.ServerID;
            this.textBoxUsername.Text = account.Username;
            this.Text = "修改账户";
            this.ShowDialog();
        }

        /// <summary>
        /// 增加窗体单击确定
        /// </summary>
        public event AddAcountFormOK OnAddOK;
        /// <summary>
        /// 修改窗体单击确定
        /// </summary>
        public event AddAcountFormOK OnEditOK;

        //退出
        private void button2_Click(object sender, EventArgs e)
        {
            this.account = null;
            this.accountList = null;
            this.textBoxChanel.Text = "";
            this.textBoxCharacterName.Text = "";
            this.textBoxPassword.Text = "";
            this.textBoxSecureCode.Text = "";
            this.textBoxServerID.Text = "";
            this.textBoxUsername.Text = "";
            this.Hide();
        }

        //确定
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (this.accountList != null)//增加
            {
                if (this.textBoxCharacterName.Text.Length == 0)
                {
                    MessageBox.Show("角色名必须输入！");
                    return;
                }

                Account account = new Account();
                account.Character = new Characters();
                account.Chanel = this.textBoxChanel.Text;
                account.Password = this.textBoxPassword.Text;
                account.SecureCode = this.textBoxSecureCode.Text;
                account.ServerID = this.textBoxServerID.Text;
                account.Username = this.textBoxUsername.Text;
                account.Character.Name = this.textBoxCharacterName.Text;
                this.accountList.Add(account);
                if (OnAddOK != null)
                {
                    OnAddOK(account);
                }
                button2_Click(null, null);
            }
            else if (this.account != null)//修改
            {
                if (this.textBoxCharacterName.Text.Length == 0)
                {
                    MessageBox.Show("角色名必须输入！");
                    return;
                }

                this.account.Chanel = this.textBoxChanel.Text;
                this.account.Character.Name = this.textBoxCharacterName.Text;
                this.account.Password = this.textBoxPassword.Text;
                this.account.SecureCode = this.textBoxSecureCode.Text;
                this.account.ServerID = this.textBoxServerID.Text;
                this.account.Username = this.textBoxUsername.Text;
                if (OnEditOK != null)
                {
                    OnEditOK(account);
                }
                button2_Click(null, null);
            }
        }

        //隐藏窗体
        private void AddAcountForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                button2_Click(null, null);
            }
        }
    }
}
